<template>
	<view>
		<view class="content"  v-if="indexOrDetial">
			<view class="member-cont">
				<view class="title-box">
					预约信息
				</view>
				<view class="staff_box form-box">
					<view class="order-box">
						<view class="right-box2">
							<view class="right-item">
								<view class="item-left">预约场地</view>
								<view class="item-right">{{memberInfo.place_id}}号场地</view>
							</view>
							<view class="right-item">
								<view class="item-left">预约时间</view>
								<view class="item-right">{{memberInfo.descri}}</view>
							</view>
		
						</view>
					</view>
		
				</view>
		
				<view class="title-box">
					添加预约
				</view>
				<view class="staff_box form-box">
					<view class="order-box">
						<view class="right-box2">
							<uni-forms ref="form" :modelValue="userFormData" label-width="100">
								<uni-forms-item label="同行人员姓名">
									<uni-easyinput type="text" v-model="userFormData.name" placeholder="请输入姓名" />
								</uni-forms-item>
								<uni-forms-item>
									<view class="btn-box">
										<button size="mini" plain="true" @click="addMember('form')">
											新增
										</button>
									</view>
								</uni-forms-item>
		
							</uni-forms>
		
						</view>
					</view>
		
				</view>
		
		
				<view class="title-box2" v-if="list.length>0">
					同行人员信息
				</view>
				<uni-card v-if="list.length>0">
					<uni-list :border="false">
						<uni-list-item :border="false" v-for="(item,index) in list" :key="index" :index="index" title="姓名"
							:rightText="item.name"></uni-list-item>
					</uni-list>
				</uni-card>
		
		
			</view>
			<view class="btn-group">
				
				<view class="btn-item">
					<button class="cancel-btn" @click="cancelData">取消预约</button>
				</view>
				<view class="btn-item">
					<button class="submimt-btn" @click="submitData">提交预约</button>
				</view>
				<view class="btn-item">
					<button class="check-btn" @click="check">查看预约</button>
				</view>
		
			</view>
		</view>
		
		<view class="detail " v-else>
			<view class="detail-top">
				<view class="info">预约详情</view>
				<uni-icons type="closeempty" size="26" @click="close"></uni-icons>
			</view>
			<uni-card v-for="(item,index) in detailList" :key="index" :index="index">
				<uni-list :border="false">
					<uni-list-item :border="false" title="预约人姓名" :rightText="item.name"></uni-list-item>
					<uni-list-item :border="false" title="预约场地" :rightText="item.place_id + '号场地'"></uni-list-item>
					<uni-list-item :border="false" title="预约时间" :rightText="item.descri"></uni-list-item>
					<view class="line-box"></view>
					<uni-list-item :border="false" title="同行人姓名" :rightText="item2.name"  v-for="(item2,index2) in item.retinue_js" :key="index2" :index="index2"></uni-list-item>
				</uni-list>
		
			</uni-card>
		</view>
	</view>
	

</template>

<script>
	import {
		getVip,
		confirm,
		cancel,
		get_order
	} from '@/api/api';

	import utils from "@/utils/utils";

	let phoneReg = /^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/;
	let idreg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;

	export default {
		data() {
			return {
				indexOrDetial: true,
				remarks: "",
				name: "",
				cardno: "",
				type: "",
				memberInfo: {},
				userFormData: {
					name: "",
				},
				numList: [1, 2, 3, 4, 5, 6, 7, 8],
				numIndex: 0,
				list: [],
				detailList: []


			}
		},
		onShow() {
			//隐藏返回首页按钮，左上角home图标
			uni.hideHomeButton();
		},
		onLoad(option) {
			console.log("option", option)
			if (option) {
				this.cardno = option.cardno
				this.type = option.type
				this.name = option.name
				this.getVipData();
			}
		},
		methods: {
			close() {
				this.indexOrDetial = true;
			},
			check() {
				
				this.getDetailList();
				this.indexOrDetial = false;
			},
			getDetailList() {
				let params = { // 卡号
					vip_or_employee: this.cardno,
				}
				get_order(params).then((res) => {
					console.log("res", res)
					if (res.msg == "success") {
						this.detailList = res.data || [];
					}
				});
			},
			async getVipData() {
				const res = await getVip(this.cardno);
				this.memberInfo = res.data[0] || [];
			},
			numItemChange(e) {
				this.numIndex = e.detail.value
			},
			checkData() {
				if (this.userFormData.name == "") {
					utils.showMessage("姓名不能为空！");
					return false
				}


				return true;

			},
			addMember() {
				let flag = this.checkData();
				if (!flag) {
					return;
				}

				if (this.list.length < 8) {
					const obj = {}
					let row = this.userFormData
					Object.keys(this.userFormData).forEach((key) => {
						obj[key] = row[key]
					})


					this.list.unshift(obj)

					this.userFormData.name = "";
				} else {
					utils.showMessage("最多可预约8人！");
					this.userFormData.name = "";
				}

			},
			submitData() {

				let params = {
					name: this.name,
					// 0 员工  1会员
					type: Number(this.type),
					// 卡号
					vip_or_employee: this.cardno,
					place_id: this.memberInfo.place_id,
					time_id: this.memberInfo.time_id,
					retinue_js: this.list
				}

				confirm(JSON.stringify(params)).then((res) => {
					console.log("confirm---res---", res)
					
					this.list = [];
					
					if(res.msg == "success"){
						
						utils.showMessage("预约成功！");
					}
				});

			},
			cancelData() {
				let params = {
					name: this.name,
					// 卡号
					vip_or_employee: this.cardno,
				}

				utils.showModal({
					title: "取消预约",
					content: "是否取消预约?",
					cancelText: "否",
					confirmText: "是",
					// confirmColor: "#576B95",
					success: async function(res1) {
						if (res1.confirm) {
							cancel(JSON.stringify(params)).then((res) => {
								console.log("res", res)
								this.list = [];
								if(res.msg == "success"){
									this.list = [];
									utils.showMessage("取消成功！");
								}
							})

						} else if (res1.cancel) {
							console.log("用户点击取消");
						}
					},
				});

			}

		}
	}
</script>

<style scoped lang="scss">
	.content {
		position: relative;
	}
	.detail {
		background: rgb(237, 237, 237);
		padding-bottom: 40rpx;
		min-height: 100vh;
		box-sizing: border-box;
	}
	
	.detail-top {
		display: flex;
		justify-content: space-between;
		padding-right: 30rpx;
	}

	.line-box{
		height: 1px;
		transform: scaleY(0.5);
		background-color: #c8c7cc;
	}
	.member-cont {
		background: rgb(237, 237, 237);
		min-height: 100vh;
		padding-bottom: 60px;
		box-sizing: border-box;
	}

	.right-item {
		display: flex;
		line-height: 88upx;
		height: 88upx;

		.item-left {
			width: 100px;
		}

	}

	.form-box {
		padding-left: 30upx;
		padding-right: 30upx;
		max-width: 750upx;
		width: 100%;
		box-sizing: border-box;
		font-size: 14px;
		color: #333;
	}

	.title-box ,.detail-top{
		line-height: 80upx;
		height: 80upx;
		font-size: 14px;
		padding-left: 40upx;
		box-sizing: border-box;
	}

	.title-box2 {
		// line-height: 70upx;
		height: 60upx;
		font-size: 14px;
		padding-left: 40upx;
		box-sizing: border-box;
		display: flex;
		justify-content: flex-start;
		align-items: flex-end;
	}

	.btn-box {
		// display: flex;
		// justify-content: center;

		button {
			margin-top: 10px;
			// margin-bottom: 10px;
			background-color: #fff;
			border-color: #3da8f5;
			color: #3da8f5;
		}
	}

	.order-box {
		background: #fff;
		border-radius: 5px;
		padding: 24rpx;
	}

	.btn-group {
		
		display: flex;
		width: 100%;
		height: 100rpx;
		background: #fff;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 100;
		border-top: 1px solid #ebebeb;
	
		.btn-item {
			flex: 1;
			padding: 15rpx;
		}
		
		.cancel-btn {
			background-color: #f56c6c;
			color: #fff;
			font-size: 15px;
			display: flex;
			justify-content: center;
			align-items: center;
		}
		
		.submimt-btn {
			background-color: #3da8f5;
			color: #fff;
			font-size: 15px;
			display: flex;
			justify-content: center;
			align-items: center;
		}
		
		.check-btn {
			background-color: #67C23A;
			color: #fff;
			font-size: 15px;
			display: flex;
			justify-content: center;
			align-items: center;
		
		}

	}
</style>
